﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YY.UI.BaseDataSvc;
using YY.Common;

namespace YY.UI
{
    public partial class EditUserFrm : BaseFrm
    {
        /// <summary>
        /// 编辑模式时传入id
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// 用户临时变量
        /// </summary>
        TB_User _User = null;

        /// <summary>
        /// 用户源数据临时变量
        /// </summary>
        IEnumerable<TB_User> users = null;

        /// <summary>
        /// 是否编辑模式
        /// </summary>
        public bool EditMode { get; set; }

        public EditUserFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new YY.UI.BaseDataSvc.BaseDataClient();
        }

        private void bindData(int id)
        {
            this._User = this.BaseDataProxy.GetUser(id);
            if (this._User != null)
            {
                this.txtBoxCode.Text = this._User.code;
                this.txtBoxPost.Text = this._User.post;
                this.txtBoxPwd.Text = this._User.pwd;
                this.txtBoxTel.Text = this._User.tel;
                this.txtBoxMobile.Text = this._User.mobile;
                this.txtBoxUserName.Text = this._User.userName;
                this.cbBoxRole.Text = Enum.GetName(typeof(EnumCommon.SysRoleType), this._User.role);
                this.cbBoxSex.Text = this._User.sex;
                if (this._User.leader != null)
                    this.cbBoxLeader.SelectedItem = this.users.SingleOrDefault(p => p.id == this._User.leader);
            }
        }

        private void EditUserFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            Common.BindEnumToComboBox(this.cbBoxRole, typeof(EnumCommon.SysRoleType), EnumCommon.SysRoleType.客服员.ToString());
            this.cbBoxSex.SelectedIndex = 0;

            this.users = this.BaseDataProxy.FindUsers(null, null);
            Common.ComboBoxBindDatas<TB_User>(this.cbBoxLeader, this.users, "userName", "id");
            this.cbBoxLeader.KeyPress += new KeyPressEventHandler(cbBoxLeader_KeyPress);
            if (this.EditMode)
            {
                bindData(this.ID);
                this.txtBoxCode.Enabled = false;
                this.txtBoxPwd.Enabled = false;
            }
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            addNew();
        }

        private void addNew()
        {
            Common.ClearForm(this.groupBox1, new Type[] { typeof(ComboBox) });
            this.EditMode = false;
            this.ID = 0;
            this._User = null;
            this.txtBoxCode.Enabled = true;
            this.txtBoxPwd.Enabled = true;
        }

        private void tsBtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtBoxCode.Text))
            {
                MessageBox.Show("请输入用户账号！");
                this.txtBoxCode.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtBoxUserName.Text))
            {
                MessageBox.Show("请输入用户名称！");
                this.txtBoxUserName.Focus();
                return;
            }
            TB_User user = new TB_User();
            user.code = this.txtBoxCode.Text;
            user.post = this.txtBoxPost.Text;

            user.role = (int)Enum.Parse(typeof(EnumCommon.SysRoleType), this.cbBoxRole.Text);
            user.sex = this.cbBoxSex.Text;
            user.tel = this.txtBoxTel.Text;
            user.mobile = this.txtBoxMobile.Text;
            user.userName = this.txtBoxUserName.Text;
            if (this.cbBoxLeader.SelectedItem != null)
                user.leader = (this.cbBoxLeader.SelectedItem as TB_User).id;

            Common.OperateType opType = Common.OperateType.Add;
            bool ok = false;
            if (this.EditMode)
            {
                user.id = this._User.id;
                user.pwd = this._User.pwd;
                user.deleteFlag = this._User.deleteFlag;
                opType = Common.OperateType.Update;

                ok = this.BaseDataProxy.UpdateUser(user);

            }
            else
            {
                user.pwd = string.IsNullOrEmpty(this.txtBoxPwd.Text) ? null : EncryptCommon.MD5Encrypt(this.txtBoxPwd.Text);
                if (this.BaseDataProxy.ExistUserCode(user.code))
                {
                    MessageBox.Show("用户账号已存在，请另起账号！");
                    return;
                }
                user.deleteFlag = false;
                user.id = this.BaseDataProxy.AddUser(user);
                ok = user.id > 0;
            }

            if (ok)
            {
                this._User = user;
                this.ID = user.id;
                this.EditMode = true;
                CommonEventArg<TB_User> arg = new CommonEventArg<TB_User>(user, opType);
                YY.Common.EventCommon.OnUserUdDGVEvnt(sender, arg);
            }
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除该项吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (this.EditMode && this.ID > 0)
                {
                    if (this.BaseDataProxy.DeleteUser(this.ID))
                    {
                        CommonEventArg<TB_User> arg = new CommonEventArg<TB_User>(this._User, Common.OperateType.Delete);
                        YY.Common.EventCommon.OnUserUdDGVEvnt(sender, arg);
                        MessageBox.Show("用户资料已删除！");
                        addNew();
                    }
                }
            }
        }

        private void cbBoxLeader_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                ComboBox cbBox = sender as ComboBox;
                if (this.users != null)
                {
                    TB_User[] users = this.users.Where(p => p.userName.Contains(cbBox.Text) || p.code.Contains(cbBox.Text)).ToArray();
                    Common.ComboBoxMatchUser(cbBox, users, this);
                }
            }
        }
    }
}
